<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Log;
use App\MediaReg;
use App\MeetingDetail;
use App\MeetingList;
use App\MeetingReg;
use App\Report;
use App\VIPReg;
use App\Volunteer;
use App\Ydyl;
use App\StuffReg;
use Carbon\Carbon;
use Illuminate\Http\Request;


// 添加SQL查询DB
use DB;
use App\MeetingRegNew;
use Illuminate\Database\QueryException;

class YDYLController extends Controller
{
    //会议注册-旧
    public function register(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $name = $data['name'];
        $phone = $data['phone'];
        $company = $data['company'];
        $meeting = $data['meeting'];
        $reg = new Ydyl();
        $reg->user_id = $user_id;
        $reg->name = $name ;
        $reg->phone = $phone;
        $reg->company = $company;
        $reg->meeting = $meeting;
        $reg->save();
        $register = Ydyl::where("user_id",$user_id)->get();
        return response()->json(['data'=>$register]);

    }
    //检索我的报名
    public function check(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
	/*
		user type:
			|_2: 普通用户
			|_3: VIP
			|_4: media
			|_5: stauff && volunteer
	*/
        $reg = MeetingRegNew::where("user_id", $user_id)->orderBy('created_at','DESC')->get();
	#Log::debug(json_encode($reg));
        #$vol = Volunteer::where("user_id",$user_id)->orderBy('created_at','DESC')->get();
 	#$vip = VIPReg::where("user_id",$user_id)->orderBy('created_at','DESC')->get();
 	#$media = MediaReg::where("user_id",$user_id)->orderBy('created_at','DESC')->get();

        return response()->json(['data'=>$reg,'data1'=>$vol,'data2'=>$vip,'data3'=>$media]);
    }
    //获取会议列表
    public function GetMeetingList(Request $request)
    {
        $data = $request->input();
        $meeting = MeetingList::where("is_checked", 1)->get();
        return response()->json(['data'=>$meeting]);
    }
    //获取会议详情
    public function GetMeetingDetail(Request $request)
    {
        $data = $request->input();
        $id = $data['meeting_id'];
        $detail = MeetingDetail::where("meeting_id",$id)->get();
        $meeting = MeetingList::where("id",$id)->first();
        return response()->json(['detail'=>$detail,'data'=>$meeting]);
    }
   //获取互联网报告
    public function GetReport(Request $request)
    {
        $data = $request->input();
        $type = $data['type'];
        if($type == 'all')
        {
            $report = Report::get();
            return response()->json(['data'=>$report]);
        }
        $report=Report::where("type",$type)->get();
        return response()->json(['data'=>$report]);
    }
    //获取最新报告
    public function GetLastReport()
    {
        $report = Report::orderBy('created_at','DESC')->get();
        return response()->json(['data' => $report]);
    }
    //获取年份报告
public function GetYearReport(Request $request)
    {
        $data = $request->input();
        $year = $data['year'];
        $type = $data['type'];
        if($year=='all')
        {
            if($type=='all')
            {
                $report = Report::get();
                return response()->json(['data'=>$report]);
            }
            $report = Report::where("type",$type)->get();
            return response()->json(['data'=>$report]);
        }
        if($type=='all')
        {
            if($year=='all')
            {
                $report = Report::get();
                return response()->json(['data'=>$report]);
            }
            $time = new Carbon($year);
            $beginTime= $time;
            $beginTime->month = 01;
            $beginTime->day = 01;
            $beginTime->hour = 00;
            $beginTime->minute = 00;
            $beginTime->second = 00;
            $begin = $beginTime->toDateTimeString();

            $endTime = $time;
            $endTime->month = 12;
            $endTime->day = 31;
            $endTime->hour = 23;
            $endTime->minute = 59;
            $endTime->second = 59;
            $end = $endTime->toDateTimeString();

            $report = Report::whereBetween('created_at', [$begin, $end])->get();
            return response()->json(['data'=>$report]);
        }
        $time = new Carbon($year);
        $beginTime= $time;
        $beginTime->month = 01;
        $beginTime->day = 01;
        $beginTime->hour = 00;
        $beginTime->minute = 00;
        $beginTime->second = 00;
        $begin = $beginTime->toDateTimeString();

        $endTime = $time;
        $endTime->month = 12;
        $endTime->day = 31;
        $endTime->hour = 23;
        $endTime->minute = 59;
        $endTime->second = 59;
        $end = $endTime->toDateTimeString();

        $report = Report::where("type",$type)->whereBetween('created_at', [$begin, $end])->get();
        return response()->json(['data'=>$report]);
    }
    //志愿者报名接口
    public function Volunteer(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $name = $data['name'];
        $phone = $data['phone'];
        $school = $data['school'];
        $major = $data['major'];
        $grade = $data['grade'];
        $option = $data['option'];

        $temp = Volunteer::where("user_id",$user_id)->first();
        if($temp)
        {
            return 0;
        }
        $volunteer = new Volunteer();
        $volunteer->user_id = $user_id;
        $volunteer->name = $name;
        $volunteer->phone = $phone;
        $volunteer->school = $school;
        $volunteer->major = $major;
        $volunteer->grade = $grade;
        $volunteer->option = $option;
        $volunteer->save();
        return 1;
    }
    //会议注册接口-第二版-已弃用
    public function MeetingReg(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $name = $data['name'];
        $sex = $data['sex'];
        $id_card = $data['id_card'];
        $phone = $data['phone'];
        $company = $data['company'];
        $meeting = $data['meeting'];

        $reg = new MeetingReg();
        $reg->user_id = $user_id;
        $reg->name = $name ;
        $reg->sex = $sex;
        $reg->id_card = $id_card;
        $reg->phone = $phone;
        $reg->company = $company;
        $reg->meeting = $meeting;
        $reg->save();

        return 1;
    }
     //嘉宾注册-已弃用
       public function VIPReg(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $name = $data['name'];
        $sex = $data['sex'];
        $phone = $data['phone'];
        $company = $data['company'];
        $meeting = $data['meeting'];
        $title = $data['title'];

        $reg = new VIPReg();
        $reg->user_id = $user_id;
        $reg->name = $name ;
        $reg->sex = $sex;
        $reg->phone = $phone;
        $reg->company = $company;
        $reg->meeting = $meeting;
        $reg->title = $title;
        $reg->save();

        return 1;
    }
	//媒体注册-已弃用
    public function MediaReg(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $name = $data['name'];
        $company = $data['company'];
        $meeting = $data['meeting'];
        $email = $data['email'];

        $reg = new MediaReg();
        $reg->user_id = $user_id;
        $reg->name = $name ;
        $reg->company = $company;
        $reg->meeting = $meeting;
        $reg->email = $email;
        $reg->save();

        return 1;
    }
 	//获取我的会议
    public function GetMyMeeting(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];

	#Log::debug('user id =>' . $user_id);

        #$reg = MeetingRegNew::where("user_id", $user_id)->orderBy('created_at','DESC')->get();
	$reg = DB::select('SELECT * FROM meeting_reg_new r,meeting_list l WHERE l.id=r.meeting_id AND r.user_id='.$user_id.' ORDER By created_at DESC');	

	#Log::debug(json_encode($reg));

        #$vol = Volunteer::where("user_id",$user_id)->orderBy('created_at','DESC')->get();
        #$vip = VIPReg::where("user_id",$user_id)->orderBy('created_at','DESC')->get();
	#$stuff = StuffReg::where("user_id",$user_id)->orderBy('created_at','DESC')->get();
        #$media = MediaReg::where("user_id",$user_id)->orderBy('created_at','DESC')->get();

        return response()->json(['data'=>$reg]);
    }
	//验票接口
    public function CheckTicket(Request $request) {
	#Log::debug(json_encode($request->all()));
	
        $data = $request->input();
        $user_id = $data['user_id'];
        $meeting_res = $data['meeting'];
	$meeting_res = array_key_exists('meeting', $data) ? $data['meeting'] : '*' || '*';	
	
	#Log::debug('meeting ==>' . json_encode($meeting_res));
	
	return $this->CheckT($meeting_res, $user_id);
    }
	//验票子方法
    public function CheckT($meeting_res, $user_id) {
        $id = $user_id;
        $meeting = $meeting_res;
	$sql = 'SELECT * FROM meeting_reg_new r,meeting_list l WHERE l.id=r.meeting_id AND r.user_id='. $user_id;

	if(!$meeting_res && $meeting_res !== '') {
	  $sql .= ' AND r.meeting_id=' . $meeting_res;
	}

        $normal = DB::select($sql);

	#Log::debug(json_encode($normal));

	$recordLen = count($normal);
        if($recordLen > 0) {
		$result = $normal[0];
		$result->status = '1';
                return response()->json(['data'=>$result]);
        }
        return 0;
    }
	//会议注册-在用
    public function MeetingReg_new(Request $request) {
        $data = $request->input();

        $user_id = 	$data['user_id'];
        $name = 	$data['name'];

	$user_type = 	array_key_exists('user_type', 		$data) ? $data['user_type'] 	: NULL;	
	$id_card = 	array_key_exists('id_card', 		$data) ? $data['id_card'] 	: NULL;	
	$meeting = 	array_key_exists('meeting', 		$data) ? $data['meeting'] 	: NULL;
	$phone = 	array_key_exists('phone', 		$data) ? $data['phone'] 	: NULL;
	$sex = 		array_key_exists('sex', 		$data) ? $data['sex'] 		: NULL;
        $company = 	array_key_exists('company', 		$data) ? $data['company'] 	: NULL;
	$organization = array_key_exists('organization',	$data) ? $data['organization']	: NULL;
	$email = 	array_key_exists('email', 		$data) ? $data['email']		: NULL;
	$position = 	array_key_exists('position', 		$data) ? $data['position'] 	: NULL;
	#Log::debug(json_encode($meeting));

        $normal = MeetingRegNew::where(["user_id"=>$user_id, "meeting_id"=>$meeting])->first();
        #$vip = VIPReg::where(["user_id"=>$user_id, "meeting_id"=>$meeting])->first();
        #$media = MediaReg::where(["user_id"=>$user_id, "meeting_id"=>$meeting])->first();

        $reg = new MeetingRegNew();
        $reg->user_id = $user_id;
        $reg->name = $name ;
        $reg->sex = $sex;
        $reg->id_card = $id_card;
        $reg->phone = $phone;
        $reg->company = $company;
        $reg->meeting_id = $meeting;
        $reg->user_type = $user_type;
        $reg->organization = $organization;
        $reg->email = $email;
        $reg->position = $position;

	try {
       		$reg->save();
	}
	catch (QueryException $e) {
		#Log::debug('exception =>');
		return 0;
	}

       	return 1;

    }
	//嘉宾注册-在用
    public function VIPReg_new(Request $request) {
        $data = $request->input();
        $user_id = $data['user_id'];
        $meeting = $data['meeting'];

        $normal = MeetingReg::where(["user_id"=>$user_id,"meeting"=>$meeting])->first();
        $vip = VIPReg::where(["user_id"=>$user_id,"meeting"=>$meeting])->first();
        $media = MediaReg::where(["user_id"=>$user_id,"meeting"=>$meeting])->first();
        if($normal||$vip||$media)
        {
            return 0;
        }

        $name = $data['name'];
        $sex = $data['sex'];
        $phone = $data['phone'];
        $company = $data['company'];
        $title = $data['title'];
        $id_card = $data['id_card'];

        $reg = new VIPReg();
        $reg->user_id = $user_id;
        $reg->name = $name ;
        $reg->sex = $sex;
        $reg->phone = $phone;
        $reg->company = $company;
        $reg->meeting_id = $meeting;
        $reg->title = $title;
        $reg->id_card = $id_card;
        $reg->save();

        return 1;
    }
	//媒体注册-在用
    public function MediaReg_new(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $meeting = $data['meeting'];

        #$normal = MeetingReg::where(["user_id"=>$user_id,"meeting"=>$meeting])->first();
        $normal = MeetingReg::find(["user_id"=>$user_id, "meeting"=>$meeting])->first();
        $vip = VIPReg::where(["user_id"=>$user_id,"meeting"=>$meeting])->first();
        $media = MediaReg::where(["user_id"=>$user_id,"meeting"=>$meeting])->first();
        if($normal||$vip||$media)
        {
            return 0;
        }

        $name = $data['name'];
        $company = $data['company'];
        $id_card = $data['id_card'];
        $email = $data['email'];

        $reg = new MediaReg();
        $reg->user_id = $user_id;
        $reg->name = $name ;
        $reg->company = $company;
        $reg->meeting_id = $meeting;
        $reg->id_card = $id_card;
        $reg->email = $email;
        $reg->save();

        return 1;
    }
	//工作人员注册-在用
    public function StuffReg(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $meeting = $data['meeting'];

        $stuff = StuffReg::where(["user_id"=>$user_id,"meeting"=>$meeting])->first();
        if($stuff) {
            return 0;
        }

        $name = $data['name'];
        $sex = $data['sex'];
        $id_card = $data['id_card'];
        $phone = $data['phone'];
        $type = $data['type'];

        $reg = new StuffReg();
        $reg->user_id = $user_id;
	$reg->meeting_id = $meeting;
        $reg->name = $name;
        $reg->sex = $sex;
        $reg->id_card = $id_card;
        $reg->phone = $phone;
        $reg->type = $type;
        $reg->save();

        return 1;
    }
}

